//
//  RegDoctorListViewController.m
//  安好
//
//  Created by lianchuang on 14-12-8.
//  Copyright (c) 2014年 lianchuangbrother. All rights reserved.
//

#import "RegDoctorListViewController.h"
#import "RegDoctorCell.h"
#import "RegDoctorModal.h"
#import "FKSearchBar.h"
@interface RegDoctorListViewController ()<UITableViewDataSource,UITableViewDelegate,FKSearchBarDelegate>
{
        FKSearchBar * illSeachBar;
}


//tab
@property(nonatomic,strong)UITableView * tableView;
//加载cell的数组
@property(nonatomic,strong)NSMutableArray * dataArray;
/**
 *  下拉加载页数
 */
@property (assign, nonatomic) NSInteger loadPage;

@property (assign, nonatomic) BOOL isSearch;
/**
 *  搜索的关键字
 */
@property(nonatomic,copy)NSString * searchKeyWord;

@end

@implementation RegDoctorListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self makeNavBar];
    [self loadData];
    [self makeTableView];
    [self markIllnessSearchBar];
}

#pragma - mark searchBar
- (void)markIllnessSearchBar
{
    illSeachBar = [[FKSearchBar alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, 44)];
    illSeachBar.searchTextField.placeholder = @"医院";
    illSeachBar.fkSearchRequestDelegate = self;
    [self.view addSubview:illSeachBar];
}

#pragma mark - 导航条设置
-(void)makeNavBar
{
    self.navigationItem.title = @"医生列表";
    //左按钮--返回
    UIBarButtonItem *backBtnImg = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"公用_返回.png"] style:UIBarButtonItemStylePlain target:self action:@selector(backClicked)];
    [backBtnImg setTintColor:[UIColor whiteColor]];
    self.navigationItem.leftBarButtonItem = backBtnImg;
    
}
/**
 * 返回
 */
-(void)backClicked
{
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)loadData
{
    self.dataArray = [NSMutableArray arrayWithCapacity:0];
    self.loadPage = 1;
    [self AFNetRequestForDoctorList];

}

-(void)AFNetRequestForDoctorList
{
    [self showActionView];
    
    NSString *postURL = [NSString stringWithFormat:@"%@doctor/",kBaseURL];
      //post参数列表
    NSMutableDictionary * parametersDic = [NSMutableDictionary dictionaryWithCapacity:0];
    //把参数省id，市id，医院id，科室id
    [parametersDic setDictionary:self.regHosDepartDic];
    [parametersDic setValue:kPlatform forKey:@"platform"];
    [parametersDic setValue:kVersion forKey:@"version"];
    //页数
    [parametersDic setValue:[NSString stringWithFormat:@"%d",self.loadPage] forKey:@"page"];
    if (self.isSearch == YES) {
        [parametersDic setValue:self.searchKeyWord forKey:@"keyword"];
    }
    
    //网络请求管理者
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager POST:postURL parameters:parametersDic success:^(AFHTTPRequestOperation *operation, id responseObject) {
        MyNSLog("成功");
        [self hiddenActionView];
        NSNumber * statusNum = [responseObject objectForKey:@"status"];
        NSNumberFormatter* numberFormatter = [[NSNumberFormatter alloc] init];
        NSString * statusString = [numberFormatter stringFromNumber:statusNum];
        if ([statusString isEqualToString:@"0"]){
            MyNSLog("%@",responseObject[@"msg"]);
            [self.tableView footerEndRefreshing];
        }
        else
        {
            MyNSLog("%@", responseObject);
            NSArray * doctorArray = responseObject[@"data"];
            if (self.isSearch == YES)
            {
                if (self.dataArray.count != 0)
                {
                    [self.dataArray removeAllObjects];
                }
                
            }
            
            if (doctorArray != nil)
            {
                
                self.loadPage++;
                for (NSDictionary * dic in doctorArray)
                {
                    
                    RegDoctorModal * modal = [[RegDoctorModal alloc] initWithDic:dic];
                    [self.dataArray addObject:modal];
                }
                [self.tableView reloadData];
            }
            else if (doctorArray.count < 20)
            {
                [self.tableView footerEndRefreshing];
            }
            else
            {
                if (self.loadPage > 1)
                {
                    MyNSLog("上拉无数据");
                }
            }
        }
        [self.tableView footerEndRefreshing];
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        MyNSLog("网络失败");
        [self hiddenActionView];
        [self.tableView footerEndRefreshing];
    }];


}

-(void)makeTableView
{
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 44, kScreenWidth, kScreenHeight- 44 -iOS7TopMargin)];
    [self.view addSubview:self.tableView];
    self.tableView.backgroundColor = [ClassMethodsViewController colorWithHexString:BACK_BACKGROUNDCOLOR_16];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    //隐藏横向和竖向的滑条
    self.tableView.showsHorizontalScrollIndicator = NO;
    self.tableView.showsVerticalScrollIndicator = NO;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.tableView addFooterWithTarget:self action:@selector(AFNetRequestForDoctorList)];


}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArray.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 65;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    RegDoctorCell * cell = [tableView dequeueReusableCellWithIdentifier:@"ID"];
    if (cell == nil) {
        cell = [[[NSBundle mainBundle] loadNibNamed:@"RegDoctorCell" owner:self options:nil] lastObject];
    }
    cell.topLineImg.hidden = YES;
    
    cell.bottomLineImg.backgroundColor = [ClassMethodsViewController colorWithHexString:LINE_BACKGROUNDCOLOR_16];
    cell.frame = CGRectMake(15, 64.5, kScreenWidth-15, 0.5);
    
    if (indexPath.row == self.dataArray.count-1) {
        cell.bottomLineImg.frame = CGRectMake(0, 64.5, kScreenWidth, 0.5);
    }
    RegDoctorModal * modal = self.dataArray[indexPath.row];
    [cell.docHeaderImg setImageWithURL:[NSURL URLWithString:modal.photourl] placeholderImage:[UIImage imageNamed:@""]];
    cell.docNameLable.text = modal.doctorname;
    cell.docGoodLable.text = modal.feature;
    cell.docLevelNameLable.text = modal.doclevelname;
    cell.regPriceLable.text = [NSString stringWithFormat:@"%d元",[modal.price intValue]];
    cell.anhaoIdent.backgroundColor =[UIColor colorWithRed:82/255.0 green:216/255.0 blue:184/255.0 alpha:1];
    cell.backView.backgroundColor=[UIColor colorWithRed:254/255.0 green:195/255.0 blue:10/255.0 alpha:1];
    cell.anhaoIdent.textColor = [UIColor whiteColor];
    cell.regPriceLable.textColor = [UIColor whiteColor];
    cell.regTitleLable.textColor = [UIColor whiteColor];
    
    return cell;

}
#pragma - mark FKSearchDelegate
/**
 *  FKSearch的delegate
 */
- (void)VoiceConversion
{
    [illSeachBar.searchTextField resignFirstResponder];
    [self starVoice];
}
- (void)voice:(NSString *)voice
{
    illSeachBar.searchTextField.text = voice;
    self.isSearch = YES;
    self.loadPage = 1;
    self.searchKeyWord = voice;
    [self AFNetRequestForDoctorList];
}
- (void)SearchString
{
    [illSeachBar.searchTextField resignFirstResponder];
    self.isSearch = YES;
    self.loadPage = 1;
    self.searchKeyWord = illSeachBar.searchTextField.text;
    [self AFNetRequestForDoctorList];
    
}





@end
